<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\ProjectSingle;

/**
 * ProjectSingleSearch represents the model behind the search form of `backend\models\ProjectSingle`.
 */
class ProjectSingleSearch extends ProjectSingle
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['level', 'province', 'city', 'category', 'owner'], 'integer'],
            [['title', 'tag', 'creditor', 'detail', 'asset', 'address', 'evaluate_by', 'highlights', 'contact', 'history', 'follow', 'source', 'created_at', 'updated_at'], 'safe'],
            [['area', 'price', 'price_per_area', 'price_pre_evaluate'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ProjectSingle::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'updated_at' => SORT_DESC
                ],
            ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'level' => $this->level,
            'province' => $this->province,
            'city' => $this->city,
            'category' => $this->category,
            'area' => $this->area,
            'price_per_area' => $this->price_per_area,
            'price_pre_evaluate' => $this->price_pre_evaluate,
            'owner' => $this->owner,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'tag', $this->tag])
            ->andFilterWhere(['like', 'creditor', $this->creditor])
            ->andFilterWhere(['like', 'detail', $this->detail])
            ->andFilterWhere(['like', 'asset', $this->asset])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'evaluate_by', $this->evaluate_by])
            ->andFilterWhere(['like', 'highlights', $this->highlights])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'history', $this->history])
            ->andFilterWhere(['like', 'follow', $this->follow])
            ->andFilterWhere(['like', 'price', $this->price])
            ->andFilterWhere(['like', 'updated_at', $this->updated_at])
            ->andFilterWhere(['like', 'source', $this->source]);
        if ($this->tag) {
            $query->andWhere("find_in_set('" . $this->tag . "',`tag`)");
        }
        return $dataProvider;
    }
}
